package dbPicasso::Plugin::ObjectBrowser;

use strict;

use base qw(dbPicasso::Plugin);
use Data::Dumper;
use dbPicasso::Config;

use vars qw($WIDGETS $DB_CONN);

sub init
{
    my $class = shift;

    my $self = $class->SUPER::init(@_);
    $self = bless $self, $class;

    my $config = dbPicasso::Config->get_section('Object Browser');
    $$config{LoadOnConnect} ||= "Y";

    dbPicasso::Config->save();

    if ($$config{LoadOnConnect} eq 'Y')
    {
        $self->register("after_connect", \&init_object_browser,
                        ['ObjectTree', 'ObjectIcons', 'ObjectInfo', 'ObjectDependsBox', 'ObjectIndexesBox'])
          or return undef;
    }

    return 1;
}

sub init_object_browser
{
    my $self = shift;

    $WIDGETS = shift;
    $DB_CONN = shift;

    my $widgets = $WIDGETS;
    my $db      = $DB_CONN;

    $$widgets{ObjectTree}->set_model(undef);
    my $objects = $db->get_object_list() or return undef;
    
    #print "Objects: " . Dumper($objects);

    my $tree = Gtk2::TreeStore->new(('Glib::String') x 2);

    my %toplevel;

    my %labels = (sproc => "Procedures", systable => 'System Tables', table => "Tables", view => "Views");

    map {
        my $schema = $_;
        #print "Adding schema $schema...\n";
        $toplevel{$schema}{iter} = $tree->append(undef);    # save this iterator for later
        $tree->set_value($toplevel{$schema}{iter}, 0, $schema);
        
        map {
            my $type = $_;
            #print "Adding object type $type...\n";
            $toplevel{$schema}{$type}{iter} = $tree->append( $toplevel{$schema}{iter} );    # save this iterator for later
            $tree->set_value($toplevel{$schema}{$type}{iter}, 0, $labels{$type}, 1, $_);
            
            map {
                my $obj = $_;
                my $objname = $obj->{name};
                #print "Adding object $objname...\n";
                $toplevel{$schema}{$type}{$objname}{iter} = $tree->append( $toplevel{$schema}{$type}{iter} );    # save this iterator for later
                $tree->set_value($toplevel{$schema}{$type}{$objname}{iter}, 0, $objname,1, $type);
            } @{ $objects->{$schema}->{$type} };
        } qw(sproc table view);
        
    } sort keys %{$objects}; 

    $$widgets{ObjectTree}->set_model($tree);

    #map {
    #    map {
    #        my ($schema, $name, $type) = @{$_};
    #        $tree->set_value($tree->append($toplevel{$type}), 0, $name, 1, $type);    
    #    } @{$objects->{$_}}
    #} sort keys %{$objects};

    my $renderer = Gtk2::CellRendererText->new;
    $renderer->set("font", "Bitstream Vera Sans 8");

    # We're re-using this tree, so only append_column once, even if we're reloading the model.
    unless ($$self{ObjectTreeInit})
    {
        $$widgets{ObjectTree}->append_column(
                         Gtk2::TreeViewColumn->new_with_attributes("Database Objects", $renderer, text => 0));
    }

    $$widgets{ObjectTree}->columns_autosize;
    $$widgets{ObjectTree}->signal_connect("row_activated", \&on_ObjectTree_row_activated);

    $$self{ObjectTreeInit} = 1;

    return 1;
}

sub on_ObjectTree_row_activated
{
    my $objecttree = shift;
    my $treepath   = shift;
    my $treecolumn = shift;

    my $model = $objecttree->get_model;

    # object name
    my $iter    = $model->get_iter_from_string($treepath->to_string);
    my $objname = $model->get_value($iter, 0);
    my $objtype = $model->get_value($iter, 1);

    print "Getting information for $objtype '$objname'\n";

    dbPicasso::UI::_clear_result_grid("ObjectCodeBox");
    dbPicasso::UI::_clear_result_grid("ObjectInfoBox");
    dbPicasso::UI::_clear_result_grid("ObjectDependsBox");

    my $textbox = dbPicasso::UI->AddSourceView(undef, "ObjectCodeBox");

    $textbox->set("editable", 0);
    $textbox->get_buffer->set_text($DB_CONN->get_object_ddl($objname, $objtype));

    my $result = $DB_CONN->get_object_info($objname);
    dbPicasso::UI::_show_results($result, "ObjectInfoBox");

    my $result = $DB_CONN->get_object_depends($objname);
    dbPicasso::UI::_show_results($result, "ObjectDependsBox");

    my $result = $DB_CONN->get_object_indexes($objname);
    dbPicasso::UI::_show_results($result, "ObjectIndexesBox");
}

1;
